<template>
  <view class="">
    <view class="search-box">
      <uni-search-bar class="search-bar" @input="input" :radius='100' cancelButton='none'></uni-search-bar>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-if="searchResult.length == 0">
        <!-- 搜索历史标题-->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="30" @click='clearHistory'></uni-icons>
      </view>
        <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in histores" :key='i' @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
    
    <!-- 搜索建议 -->
    <view class="suggst-list" v-else>
      <view class="suggst-item" v-for="(item, i) in searchResult" :key='i' @click="gotoDetail(item.goods_id)">
        <view class="goods-name">{{ item.goods_name }}</view>
        <uni-icons type='arrowright' size='16'></uni-icons>
      </view>
    </view>
  </view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
        sk: '',
        // 搜索结果列表
        searchResult: [],
        // 搜索历史列表
        historyList: ['a', 'c', 'b']
			};
		},
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('sk') || '[]')
    },                                                       
    methods:{
      input(e){
        clearTimeout(this.timer)
     this.timer = setTimeout( () => {
         console.log(e.value)
         this.sk = e.value
         this.getSearchResult()
        }, 500)
      },
      async getSearchResult(){
        if(this.sk === '') return this.searchResult = []
        const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.sk })
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchResult = res.message
        this.savaSearchHistory()
      },
      gotoDetail(goods_id) {
        uni.navigateTo({
          // 指定详情页面的 URL 地址，并传递 goods_id 参数
          url: '/subpkg/goods-detail/goods-detail?goods_id=' + goods_id
        })
      },
      // 保存搜索记录
      savaSearchHistory(){
        // this.historyList.push(this.sk)
       const set = new Set(this.historyList)
       set.delete(this.sk)
       set.add(this.sk)
       this.historyList = Array.from(set)
       uni.setStorageSync('sk', JSON.stringify(this.historyList))
      },
      // 点击删除时的处理函数
      clearHistory() {
        this.historyList = []
        uni.setStorageSync('sk', '[]')
      },
      // 点击搜索历史的任何一项跳转到商品列表的处理函数
      gotoGoodsList() {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query' + 'sk'
        })
      }
    },
    computed:{
      histores(){
       return [...this.historyList].reverse()
      }
    }
	}
</script>

<style lang="scss">
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
}
.suggst-list{
    padding: 0 15px;
  .suggst-item{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 13px 0;
    font-size: 14px;
     border-bottom: 1px solid #efefef;
    .goods-name{
      // 不允许文字换行
      white-space: nowrap;
      // 超出部分隐藏
      overflow: hidden;
      // 文本超出部分使用。。。表示
      text-overflow: ellipsis;
    }
  }
}
.history-box{
  padding: 0 5px;
  .history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    .uni-tag{
      margin: 5px 5px 0 0;
    }
  }
}
</style>
